﻿using System;
using System.Collections.Generic;
using System.Data;
using viForum.Classes;
using viForum.Providers;
using visinia.SmartEngine;
using visinia.SmartEngine.Cache;

namespace viForum.Controllers
{
    public class ForumController
    {
        public static void AddForum(Forum forum)
        {
            viForumDataProvider.Provider.AddForum(forum.CategoryId, forum.Title, forum.Description);
            DataCache.DeleteCacheMultiple("vf_forum");
        }

        public static void UpdateForum(Forum forum)
        {
            viForumDataProvider.Provider.UpdateForum(forum.ForumId, forum.CategoryId, forum.Title, forum.Description);
            DataCache.DeleteCacheMultiple("vf_forum");
        }

        public static void DeleteForum(Forum forum)
        {
            viForumDataProvider.Provider.DeleteForum(forum.ForumId);
            DataCache.DeleteCacheMultiple("vf_forum");
        }

        #region CACHE KEYS
        private const string KEY_ALL_FORUMS = "vf_forums";
        private const string KEY_FORUMS_BY_CATEGORY = "vf_forums_by_category_{0}";
        private const string KEY_FORUM = "vf_forum_{0}";
        private const string KEY_FORUM_BY_URL = "vf_forumByUrl_{0}";
        private const string KEY_POST_COUNT_BY_FORUM = "vf_forum_Count_by_forum_{0}";
        #endregion

        public static List<Forum> GetForums()
        {
            List<Forum> forums = (List<Forum>)DataCache.GetCache(KEY_ALL_FORUMS);
            if (forums == null)
            {
                forums = FillForums(viForumDataProvider.Provider.GetForums());
                DataCache.SetCache(KEY_ALL_FORUMS, forums);
            }
            return forums;
        }

        public static List<Forum> GetForums(Category category)
        {
            string key = string.Format(KEY_FORUMS_BY_CATEGORY, category.CategoryId);
            List<Forum> forums = (List<Forum>)DataCache.GetCache(key);
            if (forums == null)
            {
                forums = GetForums().FindAll(p => p.CategoryId == category.CategoryId);
                DataCache.SetCache(key, forums);
            }
            return forums;
        }

        public static Forum GetForum(int ForumId)
        {
            string key = string.Format(KEY_FORUM, ForumId);
            Forum forum = (Forum)DataCache.GetCache(key);
            if (forum == null)
            {
                forum = GetForums().Find(p => p.ForumId == ForumId);
                DataCache.SetCache(key, forum);
            }
            return forum;
        }

        public static int GetPostCount(int ForumId)
        {
            string key = string.Format(KEY_POST_COUNT_BY_FORUM, ForumId);
            if (DataCache.GetCache(key) == null)
            {
                IDataReader dr = viForumDataProvider.Provider.GetPostCount(ForumId);
                int count = 0;
                while (dr.Read())
                {
                    if (dr[0] != Convert.DBNull)
                        count = (int)dr[0];
                }
                dr.Close();
                DataCache.SetCache(key, count);
            }
            return (int)DataCache.GetCache(key);
        }

        /// <summary>
        /// Generates the url for a forum
        /// </summary>
        /// <param name="forum">The forum for which to generate url</param>
        /// <returns>returns the forum url</returns>
        private static string GenerateUrl(Forum forum)
        {
            /*****************************************************************************************/
            // forum URL = Forum/ForumId/ForumTitle .aspx
            /*****************************************************************************************/
            string key = string.Format(KEY_FORUM_BY_URL, forum.ForumId);
            string URL = (string)DataCache.GetCache(key);
            if (URL == null)
            {
                string forumId = forum.ForumId.ToString();
                string title = forum.Title;
                URL = BaseController.viForumUrlSeparator + "/" + forumId + "/" + title + ".aspx";
                DataCache.SetCache(key, URL, new TimeSpan(0, 20, 0));
            }
            return URL;
        }

        #region HELPERS
        private static List<Forum> FillForums(IDataReader dr)
        {
            List<Forum> list = new List<Forum>();
            while (dr.Read())
            {
                list.Add(ActualRead(dr));
            }
            dr.Close();
            return list;
        }
        private static Forum ActualRead(IDataReader dr)
        {
            Forum forum = new Forum();
            if (dr["ForumId"] != Convert.DBNull)
                forum.ForumId = (int)dr["ForumId"];
            if (dr["CategoryId"] != Convert.DBNull)
                forum.CategoryId = (int)dr["CategoryId"];
            if (dr["Title"] != Convert.DBNull)
                forum.Title = (string)dr["Title"];
            if (dr["Description"] != Convert.DBNull)
                forum.Description = (string)dr["Description"];
            forum.Url = WebRoots.GetWebRoot + GenerateUrl(forum);
            return forum;
        }
        #endregion
    }
}
